package com.tjetc.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.tjetc.common.Result;
import com.tjetc.entity.*;
import com.tjetc.service.IActivityService;
import com.tjetc.service.IActivityTypeService;
import com.tjetc.service.ITribeService;
import com.tjetc.service.IUserActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Autowired
    private IActivityService activityService;

    @Autowired
    private IActivityTypeService activityTypeService;

    @Autowired
    private ITribeService tribeService;


    @Autowired
    private IUserActivityService userActivityService;

    @RequestMapping("/list")
    public PageInfo<Activity> list(@RequestParam(defaultValue = "1") Integer curPage,
                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                   @RequestParam(defaultValue = "0") Integer typeId,
                                   @RequestParam(defaultValue = "0") Integer tribeId,
                                   @RequestParam(defaultValue = "3") Integer applyId,
                                   @RequestParam(defaultValue = "") String name) {
        System.out.println("curPage = " + curPage);
        System.out.println("pageSize = " + pageSize);
        System.out.println("typeId = " + typeId);
        System.out.println("tribeId = " + tribeId);
        System.out.println("name = " + name);
        System.out.println("applyId = " + applyId);
        return activityService.lists(curPage, pageSize, typeId, name,tribeId,applyId);
    }


    @RequestMapping("/add")
    public Result<?> add(Activity activity, MultipartFile photo) {
        boolean i = activityService.add(activity,  photo);
        if (i) {
            return Result.success("添加成功");
        } else {
            return Result.fail("添加失败");
        }

    }


    @RequestMapping("/update")
    public Result<?> update(Activity activity,  MultipartFile photo) {
        boolean i = activityService.updates(activity,  photo);
        if (i) {
            return Result.success("修改成功");
        } else {
            return Result.fail("修改失败");
        }

    }

    @RequestMapping("/del")
    public Result<?> del(Integer id) {
        boolean i = activityService.removeById(id);
        if (i) {
            return Result.success("删除成功");
        } else {
            return Result.fail("删除失败");
        }

    }

    @RequestMapping("/check")
    public Result<?> check(Integer id) {
        boolean i = activityService.check(id);
        if (i) {
            return Result.success("成功");
        } else {
            return Result.fail("失败");
        }

    }
    @RequestMapping("/getDetail")
    public Activity getDetail(Integer id){
        Activity activity=activityService.getById(id);
        ActivityType activityType=activityTypeService.getById(activity.getTypeId());
        activity.setActivityType(activityType);
        Tribe tribe=tribeService.getById(activity.getTribeId());
        activity.setTribe(tribe);
        return activity;
    }



    @RequestMapping("/getActivity")
    public List<Activity> getActivity(Integer state,Integer uid) {
        System.out.println("state = " + state);
        if (state==1){
            //查全部
            QueryWrapper<Activity> qw=new QueryWrapper<Activity>();
            qw.eq("status",1);
            List<Activity> list=activityService.list(qw);
            for (Activity activity : list) {
                ActivityType activityType=activityTypeService.getById(activity.getTypeId());
                activity.setActivityType(activityType);
            }
            return list;
        } else{
            //state==2时
            QueryWrapper<UserActivity> qw=new QueryWrapper<UserActivity>();
            qw.eq("user_id",uid);
            qw.eq("state",1);
            List<UserActivity>  list=userActivityService.list(qw);
            List<Activity> activities = new ArrayList<>();
            for (UserActivity userActivity : list) {
                Activity activity=activityService.getById(userActivity.getAid());
                activities.add(activity);
            }
            for (Activity activity : activities) {
                ActivityType activityType=activityTypeService.getById(activity.getTypeId());
                activity.setActivityType(activityType);
            }
            return activities;


        }



    }

}
